//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Dividend.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/CashFlow.h>
using namespace Cephei::QL;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CDividend::CDividend (boost::shared_ptr<QuantLib::Dividend>& childNative, Object^ owner) : CCashFlow(CDividend::typeid)
{
#ifdef HANDLE
	_phDividend = NULL;
#endif
	_ppDividend = &childNative;
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppDividend));
}
Cephei::QL::Cashflows::CDividend::CDividend (QuantLib::Dividend& childNative, Object^ owner) : CCashFlow(CDividend::typeid)
{
#ifdef HANDLE
	_phDividend = NULL;
#endif
	_ppDividend = new boost::shared_ptr<QuantLib::Dividend> (&childNative);
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppDividend));
    _DividendOwner = owner;
    _CashFlowOwner = owner;
}

Cephei::QL::Cashflows::CDividend::CDividend (CDividend^ copy) : CCashFlow(CDividend::typeid)
{
#ifdef HANDLE
	_phDividend = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDividend = new boost::shared_ptr<QuantLib::Dividend> (copy->GetShared());
        _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppDividend));
    }
}
Cephei::QL::Cashflows::CDividend::CDividend (PLATFORM::Type^ t) : CCashFlow(CDividend::typeid)
{
#ifdef HANDLE
	_phDividend = NULL;
#endif
	if (!t->IsSubclassOf(CDividend::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CDividend::CDividend (QuantLib::Handle<QuantLib::Dividend>& childNative, Object^ owner)  : CCashFlow(CDividend::typeid)
{
	_phDividend = &childNative;
	_ppDividend = &static_cast<boost::shared_ptr<QuantLib::Dividend>>(childNative.currentLink());
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppDividend));
    _DividendOwner = owner;
}
Cephei::QL::Cashflows::CDividend::CDividend (QuantLib::Handle<QuantLib::Dividend> childNative)  : CCashFlow(CDividend::typeid)
{
	_phDividend = &childNative;
	_ppDividend = &static_cast<boost::shared_ptr<QuantLib::Dividend>>(childNative.currentLink());
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppDividend));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CDividend::CDividend (QuantLib::Dividend childNative)  : CCashFlow(CDividend::typeid)
{
#ifdef HANDLE
	_phDividend = NULL;
#endif
	_ppDividend = new boost::shared_ptr<QuantLib::Dividend> (new QuantLib::Dividend (childNative));
    _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppDividend));
}
#endif

Cephei::QL::Cashflows::CDividend::~CDividend ()
{
    if (_ppDividend != NULL)
    {
	    delete _ppDividend;
        _ppDividend = NULL;
    }
}
Cephei::QL::Cashflows::CDividend::!CDividend ()
{
    if (_ppDividend != NULL)
    {
	    delete _ppDividend;
    }
}
QuantLib::Dividend& Cephei::QL::Cashflows::CDividend::GetReference ()
{
    if (_ppDividend == NULL) throw REFNEW NativeNullException ();
	return **_ppDividend;
}
boost::shared_ptr<QuantLib::Dividend>& Cephei::QL::Cashflows::CDividend::GetShared ()
{
    if (_ppDividend == NULL) throw REFNEW NativeNullException ();
	return *_ppDividend;
}
QuantLib::Dividend* Cephei::QL::Cashflows::CDividend::GetPointer ()
{
    if (_ppDividend == NULL) throw REFNEW NativeNullException ();
	return &**_ppDividend;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Dividend>& Cephei::QL::Cashflows::CDividend::GetHandle ()
{
	if (_phDividend == NULL)
	{
		_phDividend = new Handle<QuantLib::Dividend> (*_ppDividend);
	}
	return *_phDividend;
}
#endif
bool Cephei::QL::Cashflows::CDividend::HasNative () 
{
	return (_ppDividend != NULL);
}

Double Cephei::QL::Cashflows::CDividend::Amount (Double underlying)
{
    try
    {
        QuantLib::Real _underlying = (QuantLib::Real)ValueHelper::Convert (underlying); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppDividend)->amount ( _underlying );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CDividend::Amount ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppDividend)->amount ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Cashflows::CDividend::Date::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppDividend)->date ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Cashflows::CDividendVector::CDividendVector ()
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
}
Cephei::QL::Cashflows::CDividendVector::~CDividendVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
	_pVValue = NULL;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
	_pVBoost = NULL;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
	_pVHand = NULL;
#endif
}
Cephei::QL::Cashflows::CDividendVector::!CDividendVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
#endif
}
#ifdef STRUCT
Cephei::QL::Cashflows::CDividendVector::CDividendVector (std::vector<QuantLib::Dividend>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = &vec;
	_owner = owner;
}
Cephei::QL::Cashflows::CDividendVector::CDividendVector (std::vector<QuantLib::Dividend> vec)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = new std::vector<QuantLib::Dividend> (vec);
}
#endif
Cephei::QL::Cashflows::CDividendVector::CDividendVector (std::vector<boost::shared_ptr<QuantLib::Dividend>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVBoost = &vec;
	_owner = owner;
}
#ifdef HANDLE
Cephei::QL::Cashflows::CDividendVector::CDividendVector (std::vector<QuantLib::Handle<QuantLib::Dividend>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVHand = &vec;
	_owner = owner;
}
#endif

int Cephei::QL::Cashflows::CDividendVector::Count::get ()
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
		return _pVValue->size();
	}
#endif    
	if (_pVBoost != NULL)
	{
		return _pVBoost->size ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		return _pVHand->size ();
	}
#endif
	return 0;		// should never happen
}
Cephei::QL::Cashflows::IDividend^ Cephei::QL::Cashflows::CDividendVector::default::get (int index) 
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
    	if ((size_t)index >= _pVValue->size()) throw REFNEW RangeException ();
		QuantLib::Dividend& native = (*_pVValue)[index];
		return REFNEW CDividend (native, this);
	}
#endif    
	if (_pVBoost != NULL)
	{
    	if ((size_t)index >= _pVBoost->size()) throw REFNEW RangeException ();
		QuantLib::Dividend& native = *(*_pVBoost)[index];
		return REFNEW CDividend (native, this);
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
    	if ((size_t)index >= _pVHand->size()) throw REFNEW RangeException ();
		QuantLib::Dividend& native = *((*_pVHand)[index].currentLink ());
		return REFNEW CDividend (native, this);
	}
#endif
	return nullptr;		// can never be the case
}
void Cephei::QL::Cashflows::CDividendVector::default::set (int index, Cephei::QL::Cashflows::IDividend^ value) 
{
#ifdef STRUCT
	if (_pVValue != NULL)
	{
		if (_pVValue->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVValue->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVValue->push_back (dynamic_cast<CDividend^>(value)->GetReference ());
		}
        else
        {
            QuantLib::Dividend& c = (*_pVValue)[index];
            c = dynamic_cast<CDividend^>(value)->GetReference ();
        }
	}
#endif   
	if (_pVBoost != NULL)
	{
		if (_pVBoost->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVBoost->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVBoost->push_back (dynamic_cast<CDividend^>(value)->GetShared ());
		}
		boost::shared_ptr<QuantLib::Dividend>& c = (*_pVBoost)[index];
		c = dynamic_cast<CDividend^>(value)->GetShared ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		if (_pVHand->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVHand->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVHand->push_back (QuantLib::Handle<QuantLib::Dividend> ());
		}
		QuantLib::Handle<QuantLib::Dividend>& c = (*_pVHand)[index];
		boost::shared_ptr<QuantLib::Dividend>& b = static_cast<boost::shared_ptr<QuantLib::Dividend>>(c.currentLink());
		b = dynamic_cast<CDividend^>(value)->GetShared ();
	}
#endif
}
Cephei::Core::Generic::ICoCell<Cephei::QL::Cashflows::IDividend^>^ Cephei::QL::Cashflows::CDividendVector::CItem::get (int index) 
{
	return CellFactory::Instance->Create<IDividend^> (dynamic_cast<IDividend^>(default[index]));
}

void Cephei::QL::Cashflows::CDividendVector::CItem::set (int index, Cephei::Core::Generic::ICoCell<Cephei::QL::Cashflows::IDividend^>^ value) 
{
	default[index] = safe_cast<IDividend^>(value->Value);
}
#ifdef STRUCT
std::vector<QuantLib::Dividend>& Cephei::QL::Cashflows::CDividendVector::GetReference ()
{
	 return *_pVValue;
}
#endif

std::vector<boost::shared_ptr<QuantLib::Dividend>>& Cephei::QL::Cashflows::CDividendVector::GetShared ()
{
	return *_pVBoost;
}

#ifdef HANDLE
std::vector<QuantLib::Handle<QuantLib::Dividend>>& Cephei::QL::Cashflows::CDividendVector::GetHandle ()
{
	return *_pVHand;
}
#endif
bool Cephei::QL::Cashflows::CDividendVector::PrepareFeature (NativeFeature feature)
{
	switch (feature)
	{
#ifdef STRUCT
		case NativeFeature::Value :
			if (_pVValue == NULL)
			{
				_pVValue = new std::vector<QuantLib::Dividend> ();
				return true;
			}
			break;
#endif
        case NativeFeature::shared_ptr :
			if (_pVBoost == NULL)
			{
				_pVBoost = new std::vector<boost::shared_ptr<QuantLib::Dividend>> ();
				return true;
			}
			break;
#ifdef HANDLE
        case NativeFeature::Handle :
            if (_pVHand == NULL)
            {
                _pVHand = new std::vector<QuantLib::Handle<QuantLib::Dividend>> ();
                return true;
            }
            break;
#endif
	}
	return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

/* <summary>Create a single dimension array of Dividend that can be passed into other function </summary> */
IVector<Cephei::QL::Cashflows::IDividend^>^ Cephei::QL::Cashflows::CDividend_Factory::CreateVector ()
 {
	 return REFNEW CoVector<IDividend^> (REFNEW CDividendVector ());
 }
IVector<Cephei::QL::Cashflows::IDividend^>^ Cephei::QL::Cashflows::CDividend_Factory::CreateVector (PLATFORM::Collections::Generic::IEnumerable<IDividend^>^ source)
 {
	 return REFNEW CoVector<IDividend^> (source, REFNEW CDividendVector ());
 }
/* <summary>Create a single dimension array of Dividend with change events</summary> */
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Cashflows::IDividend^>^>^>^ Cephei::QL::Cashflows::CDividend_Factory::CreateCellVector ()
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Cashflows::IDividend^>^>^> (REFNEW CoCellVector<Cephei::QL::Cashflows::IDividend^> (REFNEW CDividendVector ()));
 }
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Cashflows::IDividend^>^>^>^ Cephei::QL::Cashflows::CDividend_Factory::CreateCellVector (PLATFORM::Collections::Generic::IEnumerable<Cephei::Core::Generic::ICoCell<Cephei::QL::Cashflows::IDividend^>^>^ source)
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Cashflows::IDividend^>^>^> (REFNEW CoCellVector<Cephei::QL::Cashflows::IDividend^> (source, REFNEW CDividendVector ()));
 }

